package com.huawang.mdesign.model.service.util;

import lombok.extern.java.Log;
import org.jetbrains.annotations.NotNull;

import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 生成唯一字符串的工具类
 */
@Log
public final class GenerateUniqueStringUtils {

    private GenerateUniqueStringUtils() {
    }

    @NotNull
    public static String convert(String uniqueString) {
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
            byte[] hash = messageDigest.digest(uniqueString.getBytes(StandardCharsets.UTF_8));
            BigInteger bigInteger = new BigInteger(1, hash);
            return bigInteger.longValue() + "";
        } catch (NoSuchAlgorithmException e) {
            log.warning("唯一字符串生成错误，" + e.getMessage());
            return "-1";
        }
    }
}
